//
//  SISSHistoryOperationalViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/3.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHistoryOperationalViewController.h"
#import "SISSChannelLabel.h"
#import "SISSHouseResourceModel.h"
#import "SISSHouseProgressManageCell.h"
#import "SISSRentProgressManageCell.h"
#import "SISSPassengerProgerssManageCell.h"
#import "SISSHouseCooperationProgressManageCell.h"
#import "SISSPassengerCooperationProgressManageCell.h"
#import "SISSNetworkingManager+Mine.h"
#import "SISSHouseProgressViewController.h"
#import "SISSPassengerProgressViewController.h"
#import "SISSRentOutProgressViewController.h"
#import "SISSTenantProgressViewController.h"
#import "SISSHouseCooperationProgressViewController.h"
#import "SISSPassengerCooperationProgressViewController.h"

static NSString *historyHouseCellResueID = @"historyHouseCellResueID";
static NSString *historyPassengerCellResueID = @"historyPassengerCellResueID";
static NSString *historyRentProgressCellResueID = @"historyRentProgressCellResueID";
static NSString *historyHouseCooperationCellResueID = @"historyHouseCooperationCellResueID";
static NSString *historyPassengerCooperationCellResueID = @"historyPassengerCooperationCellResueID";
@interface SISSHistoryOperationalViewController ()
@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;


/** 房源数组 */
@property (copy, nonatomic) NSArray<SISSHouseProgressModel *> *houseResourceArr;
/** 客源数组 */
@property (copy, nonatomic) NSArray<SISSPassengerProgressModel *> *passengerResourceArr;
/** 出租数组 */
@property (copy, nonatomic) NSArray<SISSRentOutProgressModel *> *rentOutResourceArr;
/** 求租数组 */
@property (copy, nonatomic) NSArray<SISSTenantProgressModel *> *tenantResourceArr;
/** 房源合作数组 */
@property (copy, nonatomic) NSArray<SISSHouseCoopProgressModel *> *houseCoopResourceArr;
/** 客源合作数组 */
@property (copy, nonatomic) NSArray<SISSPassengerCoopProgressModel *> *passengerCoopResourceArr;

/* channelIndex 行数 */
@property (assign, nonatomic) NSInteger channelIndex;

// 频道标签滚动视图
@property (weak, nonatomic) UIScrollView *channelScrollView;
/* 频道标签模型数组 */
@property (strong, nonatomic) NSArray *channels;
/* 频道标签数组 */
@property (strong, nonatomic) NSMutableArray *labelsM;

@end

@implementation SISSHistoryOperationalViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (self.channelIndex == 0) {
        [self requestHistoryHouseResourceData];
    } else if (self.channelIndex == 1) {
        [self requestHistoryPassengerResourceData];
    } else if (self.channelIndex == 2){
        [self requestHistoryRentOutResourceData];
    } else if (self.channelIndex == 3){
        [self requestHistoryTenantResourceData];
    } else if (self.channelIndex == 4){
        [self requestHistoryHouseCooperationResourceData];
    } else if (self.channelIndex == 5) {
       [self requestHistoryPassengerCooperationResourceData];
    }
 
}


#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (self.channelIndex) {
        case 0: {
            [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.houseResourceArr.count];
            return self.houseResourceArr.count;
        }
            break;
            
        case 1: {
            [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.passengerResourceArr.count];
            return self.passengerResourceArr.count;
        }
            break;
            
        case 2: {
            [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.rentOutResourceArr.count];
            return self.rentOutResourceArr.count;
        }
            break;
            
        case 3: {
            [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.tenantResourceArr.count];
            return self.tenantResourceArr.count;
        }
            break;
            
        case 4: {
            [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.houseCoopResourceArr.count];
            return self.houseCoopResourceArr.count;
        }
            break;
            
        case 5: {
            [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.passengerCoopResourceArr.count];
            return self.passengerCoopResourceArr.count;
        }
            break;
            
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (self.channelIndex) {
        case 0: {
            SISSHouseProgressManageCell *cell = [tableView dequeueReusableCellWithIdentifier:historyHouseCellResueID forIndexPath:indexPath];
            cell.isHistory = YES;
            cell.houseProgressModel = self.houseResourceArr[indexPath.row];
            return cell;
        }
            break;
            
        case 1: {
            SISSPassengerProgerssManageCell *cell = [tableView dequeueReusableCellWithIdentifier:historyPassengerCellResueID forIndexPath:indexPath];
            cell.isHistory = YES;
            cell.passengerProgressModel = self.passengerResourceArr[indexPath.row];
            return cell;
        }
            break;
        case 2:
        case 3: {
            SISSRentProgressManageCell *cell = [tableView dequeueReusableCellWithIdentifier:historyRentProgressCellResueID forIndexPath:indexPath];
            cell.isHistory = YES;
            if (self.channelIndex == 2) {
//                self.rentOutResourceArr[indexPath.row].has_news = 0;
                cell.rentOutProgressModel = self.rentOutResourceArr[indexPath.row];
            } else {
//                self.tenantResourceArr[indexPath.row].has_news = 0;
                cell.tenantProgressModel = self.tenantResourceArr[indexPath.row];
            }
            
            return cell;
        }
            break;
            
        case 4: {
            SISSHouseCooperationProgressManageCell *cell = [tableView dequeueReusableCellWithIdentifier:historyHouseCooperationCellResueID forIndexPath:indexPath];
            cell.isHistory = YES;
            self.houseCoopResourceArr[indexPath.row].has_news = 0;
            cell.houseCoopProgressModel = self.houseCoopResourceArr[indexPath.row];
            return cell;
        }
            break;
            
        case 5: {
            SISSPassengerCooperationProgressManageCell *cell = [tableView dequeueReusableCellWithIdentifier:historyPassengerCooperationCellResueID forIndexPath:indexPath];
            cell.isHistory = YES;
            self.passengerCoopResourceArr[indexPath.row].has_news = 0;
            cell.passengerCoopProgressModel = self.passengerCoopResourceArr[indexPath.row];
            return cell;
        }
            break;
            
        default:
            break;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    switch (self.channelIndex) {
        case 0: {
            SISSHouseProgressViewController *vc = [[SISSHouseProgressViewController alloc] init];
            vc.title = @"房源进度";
            vc.isHistory = YES;
            vc.houseProgressModel = self.houseResourceArr[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 1: {
            SISSPassengerProgressViewController *vc = [[SISSPassengerProgressViewController alloc] init];
            vc.title = @"客源进度";
            vc.isHistory = YES;
            vc.passengerProgressModel = self.passengerResourceArr[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 2: {
            SISSRentOutProgressViewController *vc = [[SISSRentOutProgressViewController alloc] init];
            vc.title = @"出租进度";
            vc.isHistory = YES;
            vc.rentOutProgressModel = self.rentOutResourceArr[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];

        }
            break;
            
        case 3: {
            SISSTenantProgressViewController *vc = [[SISSTenantProgressViewController alloc] init];
            vc.title = @"求租进度";
            vc.isHistory = YES;
            vc.tenantProgressModel = self.tenantResourceArr[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 4: {
            SISSHouseCooperationProgressViewController *vc = [[SISSHouseCooperationProgressViewController alloc] init];
            vc.title = @"合作进度";
            vc.isHistory = YES;
            vc.houseCoopProgressModel = self.houseCoopResourceArr[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
            
        case 5: {
            SISSPassengerCooperationProgressViewController *vc = [[SISSPassengerCooperationProgressViewController alloc] init];
            vc.title = @"合作进度";
            vc.isHistory = YES;
            vc.passengerCoopProgressModel = self.passengerCoopResourceArr[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }

}

- (void)channelItemClickWithIndex:(NSInteger)index {
    
    self.channelIndex = index;
    switch (index) {
        case 0: {
            
            [self requestHistoryHouseResourceData];
        }
            break;
            
        case 1: {
            
            [self requestHistoryPassengerResourceData];
        }
            break;
            
        case 2: {
            
            [self requestHistoryRentOutResourceData];
        }
            break;
            
        case 3: {
            
            [self requestHistoryTenantResourceData];
        }
            break;
            
        case 4: {
            
            [self requestHistoryHouseCooperationResourceData];
        }
            break;
            
        case 5: {
            
            [self requestHistoryPassengerCooperationResourceData];
        }
            break;
            
        default:
            break;
    }
    
    [self.tableView reloadData];
}

#pragma mark - 创建频道标签的主方法
- (void)createChannelLabels {
    // 获取频道标签数组
    self.channels = @[@"房源", @"客源", @"出租", @"求租", @"房源合作", @"客源合作"];
    
    CGFloat labelW = 80;
    CGFloat labelH = 44;
    
    // 遍历频道标签模型数组,循环的创建label
    for (NSInteger i = 0; i < self.channels.count; i++) {
        // 创建频道标签控制
        SISSChannelLabel *label = [[SISSChannelLabel alloc] init];
        label.textColor = MAIN_TEXT_COLOR;
        // 添加到父控件
        [self.channelScrollView addSubview:label];
        //        labelW = [self.channels[i] boundingRectWithSize:CGSizeMake(44, 16) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}  context:nil].size.width;
        // 设置frame
        CGFloat labelX = labelW * i;
        label.frame = CGRectMake(labelX, 0, labelW, labelH);
        // 赋值
        label.text = self.channels[i];
        
        // 给每创建的一个label添加点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(channelLabelTapClick:)];
        [label addGestureRecognizer:tap];
        // 开启交互
        label.userInteractionEnabled = YES;
        // 给label添加tag值,方便点击时获取点击的label的索引
        label.tag = i;
        
        // 每创建一个label,就向数组里面添加一个
        [self.labelsM addObject:label];
        
        // 把第0个label(头条)设置为最大的缩放比
        if (i == 0) {
            label.scale = 1.0;
        }
    }
    
    // 设置channelScrollView的滚动范围
    self.channelScrollView.contentSize = CGSizeMake(labelW * self.channels.count + 10, 0);
}

#pragma mark-实现手势点击事件
- (void)channelLabelTapClick:(UITapGestureRecognizer *)tap {
    // 1.把选中的label居中
    // 获取选中的label
    SISSChannelLabel *tapLabel = (SISSChannelLabel *)tap.view;
    // 计算tapLabel滚动到居中时,需要的距离
    CGFloat offsetX = tapLabel.center.x - self.view.bounds.size.width * 0.5;
    
    // 设定最大和最小滚动范围
    CGFloat minOffsetX = 0;
    CGFloat maxOffsetX = self.channelScrollView.contentSize.width - self.view.bounds.size.width + 44;
    if (offsetX < minOffsetX) {
        offsetX = minOffsetX;
    }
    if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    // label居中时,需要滚动到的点
    CGPoint point = CGPointMake(offsetX, 0);
    // 选中label居中时需要执行的方法
    [self.channelScrollView setContentOffset:point animated:YES];
    
    // 2.把label对应的新闻视图滚动到对应的位置 : 滚动到指定的位置
    //    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:tapLabel.tag inSection:0];
    //    [self.homeCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    [self channelItemClickWithIndex:tapLabel.tag];
    // 遍历所有的标签,只把选中的标签拿出来,赋值为最大的缩放比,其他的全部赋值最小的缩放比
    for (NSInteger i = 0; i < self.labelsM.count; i++) {
        
        // 循环的一个一个的取标签
        SISSChannelLabel *label = self.labelsM[i];
        
        // 判断取出的标签,是否是选中的标签
        if (i == tapLabel.tag) {
            label.scale = 1.0;
        } else {
            label.scale = 0.0;
        }
    }
}

#pragma mark - navigationItem
- (void)setupNavigationItems {
    [super setupNavigationItems];
    
    self.labelsM = [NSMutableArray array];
    UIScrollView *channelScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    channelScrollView.showsHorizontalScrollIndicator = NO;
    channelScrollView.showsVerticalScrollIndicator = NO;
    self.channelScrollView = channelScrollView;
    self.navigationItem.titleView = channelScrollView;
    
    [self createChannelLabels];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSHouseProgressManageCell" bundle:nil] forCellReuseIdentifier:historyHouseCellResueID];
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSPassengerProgerssManageCell" bundle:nil] forCellReuseIdentifier:historyPassengerCellResueID];
    [self.tableView registerClass:[SISSRentProgressManageCell class] forCellReuseIdentifier:historyRentProgressCellResueID];
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSHouseCooperationProgressManageCell" bundle:nil] forCellReuseIdentifier:historyHouseCooperationCellResueID];
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSPassengerCooperationProgressManageCell" bundle:nil] forCellReuseIdentifier:historyPassengerCooperationCellResueID];
}

#pragma mark - 上拉刷新加载方法
- (void)upDownLoadData {
    
    switch (self.channelIndex) {
        case 0: {
            
            [self requestHistoryHouseResourceData];
        }
            break;
            
        case 1: {
            
            [self requestHistoryPassengerResourceData];
        }
            break;
            
        case 2: {
            
            [self requestHistoryRentOutResourceData];
        }
            break;
            
        case 3: {
            
            [self requestHistoryTenantResourceData];
        }
            break;
            
        case 4: {
            
            [self requestHistoryHouseCooperationResourceData];
        }
            break;
            
        case 5: {
            
            [self requestHistoryPassengerCooperationResourceData];
        }
            break;
            
        default:
            break;
    }

}

#pragma mark - 加载数据
#pragma mark - 加载房源数据
- (void)requestHistoryHouseResourceData {
    
    WeakSelf
    [kNetworkManager requestHouseProgressHistoryWithsuccess:^(NSArray<SISSHouseProgressModel *> *houseProgressArr) {
        
        weakSelf.houseResourceArr = houseProgressArr;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - 加载客源数据
- (void)requestHistoryPassengerResourceData {
    
    WeakSelf
    [kNetworkManager requestPassengerProgressHistoryWithsuccess:^(NSArray<SISSPassengerProgressModel *> *passengerProgressArr) {
        
        weakSelf.passengerResourceArr = passengerProgressArr;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - 加载出租进度数据
- (void)requestHistoryRentOutResourceData {
    
    WeakSelf
    [kNetworkManager requestRentOutProgressHistoryWithsuccess:^(NSArray<SISSRentOutProgressModel *> *rentOutProgressArr)  {

        weakSelf.rentOutResourceArr = rentOutProgressArr;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];

    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - 加载租客进度数据
- (void)requestHistoryTenantResourceData {
    
    WeakSelf
    [kNetworkManager requestTenantProgressHistoryWithsuccess:^(NSArray<SISSTenantProgressModel *> *tenantProgressArr) {

        weakSelf.tenantResourceArr = tenantProgressArr;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];

    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark -- 加载房源合作数据
- (void)requestHistoryHouseCooperationResourceData {
    
    WeakSelf
    [kNetworkManager requestHouseCooperationHistoryWithsuccess:^(NSArray<SISSHouseCoopProgressModel *> *houseCoopArr) {
        
        weakSelf.houseCoopResourceArr = houseCoopArr;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark -- 加载客源合作数据
- (void)requestHistoryPassengerCooperationResourceData {
    
    WeakSelf
    [kNetworkManager requestPassengerCooperationHistoryWithsuccess:^(NSArray<SISSPassengerCoopProgressModel *> *passengerCoopArr) {
        
        weakSelf.passengerCoopResourceArr = passengerCoopArr;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}


@end
